package com.example.function_interface.operator;

import com.example.function_interface.User;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.BinaryOperator;

/**
 * 代表了一个作用于于两个同类型操作符的操作，并且返回了操作符同类型的结果
 */
public class BinaryOperatorTest {

    @Test
    public void myTest() throws Exception{
        List<User> userList = new ArrayList<>();
        userList.add(new User(1, "张三", 12, false));
        userList.add(new User(2, "李四", 22, false));
        userList.add(new User(3, "王五", 32, true));
        userList.add(new User(4, "郑静", 2, true));

        BinaryOperator binaryOperator = new BinaryOperator() {
            @Override
            public Object apply(Object o, Object o2) {
                return null;
            }
        };
        Comparator<User> comparator = (user1, user2) -> user1.getAge() > user2.getAge() ? 1 : -1;
        BinaryOperator<User> maxBy = BinaryOperator.maxBy(comparator);
        BinaryOperator<User> minBy = (user1, user2) -> user1.getAge() > user2.getAge() ? user2 : user1;

        System.out.println(maxBy.apply(userList.get(1), userList.get(2)).getName());
        System.out.println(minBy.apply(userList.get(1), userList.get(2)).getName());

    }
}
